package com.chuangshi.core.vote.player.controller.admin;

import com.chuangshi.core.kit.Controller;
import com.chuangshi.core.kit.IdWorker;
import com.chuangshi.core.kit.Result;
import com.chuangshi.core.vote.player.entity.Player;
import com.chuangshi.core.vote.player.controller.admin.request.*;
import com.chuangshi.core.vote.player.controller.admin.response.*;
import com.chuangshi.core.vote.player.entity.column.PlayerColumn;
import com.chuangshi.core.vote.player.service.PlayerService;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 人员信息管理端控制器
 *
 * @author ggliming
 * @date 2019-02-21
 */
@Api(tags = "人员信息管理端接口管理")
@RestController
public class PlayerAdminController extends Controller {

    @Autowired
    private PlayerService playerService;

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "获取人员信息列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerAdminV1ListRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = PlayerAdminV1ListResponse.class)
    })
    @RequestMapping(value = "/vote/player/admin/v1/list", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1List(@ApiIgnore @RequestBody PlayerAdminV1ListRequest body) {
        validateRequest(body);

        Result result = playerService.listForAdmin(body);

        validateResponse(PlayerAdminV1ListResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "获取单条人员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerAdminV1FindRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = PlayerAdminV1FindResponse.class)
    })
    @RequestMapping(value = "/vote/player/admin/v1/find", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Find(@ApiIgnore @RequestBody PlayerAdminV1FindRequest body) {
        validateRequest(body);

        Player result = playerService.find(body.getPlayerId());

        validateResponse(PlayerAdminV1FindResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "新增人员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerAdminV1SaveRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "新增成功")
    })
    @RequestMapping(value = "/vote/player/admin/v1/save", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Save(@ApiIgnore @RequestBody PlayerAdminV1SaveRequest body) {
        validateRequest(body);

        Player player = new Player(body);

        String playerId = IdWorker.getIdStr();
        player.setPlayerId(playerId);
        player.setPlayerVoteNum(0);

        Boolean result = playerService.save(player);

        return renderJson(result, result ? "新增成功" : "新增不成功");
    }

    @ApiOperationSort(value = 40)
    @ApiOperation(value = "修改人员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerAdminV1UpdateRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功")
    })
    @RequestMapping(value = "/vote/player/admin/v1/update", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Update(@ApiIgnore @RequestBody PlayerAdminV1UpdateRequest body) {
        validateRequest(body);

        Player player = new Player(body);

        Boolean result = playerService.update(player, body.getSystemVersion());

        return renderJson(result, result ? "修改成功" : "修改不成功");
    }

    @ApiOperationSort(value = 50)
    @ApiOperation(value = "删除人员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerAdminV1DeleteRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功")
    })
    @RequestMapping(value = "/vote/player/admin/v1/delete", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Delete(@ApiIgnore @RequestBody PlayerAdminV1DeleteRequest body) {
        validateRequest(body);

        Boolean result = playerService.delete(body.getPlayerIdList());

        return renderJson(result, result ? "删除成功" : "删除不成功");
    }

}